package com.example.aslongastoday.util;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkHttpUtil {
    private static ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(2,5,10, TimeUnit.SECONDS,new ArrayBlockingQueue<>(5));

    public static Future<JsonElement> login(String url,String postBody) throws InterruptedException {
        OkHttpClient okHttpClient = new OkHttpClient();

        RequestBody requestBody = RequestBody.create(postBody, MediaType.parse("application/json"));
        Request request = new Request.Builder().
                url(url).
                post(requestBody).build();
        Call call = okHttpClient.newCall(request);

        Future<JsonElement> future = poolExecutor.submit((Callable<JsonElement>) () -> {
            Response response = null;
            JsonObject jsonObject = null;
            try {
                response = call.execute();
                String result = response.body().string();
                jsonObject = JsonParser.parseString(result).getAsJsonObject();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return jsonObject.get("code");
        });
        return future;
    }


    public static Future<JsonElement> get(String url) throws InterruptedException {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().
                url(url).build();

        Future<JsonElement> future = poolExecutor.submit((Callable<JsonElement>) () -> {
            Response response = null;
            JsonObject jsonObject = null;
            try {
                response = okHttpClient.newCall(request).execute();

                String result = response.body().string();
                jsonObject = JsonParser.parseString(result).getAsJsonObject();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return jsonObject.get("data");
        });

        return future;
    }



    public static Future<JsonElement> post(String url, String postBody){
        OkHttpClient okHttpClient = new OkHttpClient();

        RequestBody requestBody = RequestBody.create(postBody, MediaType.parse("application/json"));
        Request request = new Request.Builder().
                url(url).
                post(requestBody).build();
        Call call = okHttpClient.newCall(request);

        Future<JsonElement> future = poolExecutor.submit((Callable<JsonElement>) () -> {
            Response response = null;
            JsonObject jsonObject = null;
            try {
                response = call.execute();
                String result = response.body().string();
                jsonObject = JsonParser.parseString(result).getAsJsonObject();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return jsonObject.get("data");
        });
        return future;
    }

}
